/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.BusinessLogicLayer;

import edu.nii.qlks.DataAccessLayer.MSSQLConnectUnit;
import edu.nii.qlks.DataTransferObject.PhongInfo;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;

/**
 *
 * @author MARS_NHO
 */
public class PhongBLL {
    
    MSSQLConnectUnit _connect;
    ResultSet  _result;
 
    
    public ArrayList LayDS(String Condition, String OderBy) throws Exception{
        ArrayList vt = new ArrayList();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("PHONG", Condition, OderBy);
        
        while (_result.next()){
            ArrayList vt_row = new ArrayList();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt_row.add(_result.getString(5));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public ArrayList LayDS(String Condition) throws Exception{
         return LayDS(Condition, null);
    }
    
    public ArrayList LayDS() throws Exception{
        return LayDS(null);
    }
    
    public void Insert(PhongInfo phongInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("MaPhong", phongInfo.getMaPhong());
        map.put("TenPhong", phongInfo.getTenPhong());
        map.put("MaLoaiPhong", phongInfo.getMaLoaiPhong());
        map.put("TinhTrangPhong", phongInfo.isTinhTrangPhong());
        map.put("GhiChu", phongInfo.getGhiChu());
        _connect.Insert("PHONG", map);
    }
    
     
    public void Inserts(HashSet<PhongInfo> phongInfos) throws Exception{
        for (PhongInfo phongInfo:phongInfos) {
            this.Insert(phongInfo);
        }
    }
    
    public void Update(PhongInfo phongInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("TenPhong", phongInfo.getTenPhong());
        map.put("MaLoaiPhong", phongInfo.getMaLoaiPhong());
        map.put("TinhTrangPhong", phongInfo.isTinhTrangPhong());
        map.put("GhiChu", phongInfo.getGhiChu());
        _connect.Update("PHONG", map, "MaPhong = " + phongInfo.getMaPhong());
    }
    
    public void Updates(HashSet<PhongInfo> phongInfos) throws Exception{
        for (PhongInfo phongInfo:phongInfos){
            this.Update(phongInfo);
        }
    }
    
    public void Delete(PhongInfo phongInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _connect.Delete("PHONG", "MaPhong = " + phongInfo.getMaPhong());
    }
    
    public void Deletes(HashSet<PhongInfo> phongInfos) throws Exception{
         for (PhongInfo phongInfo:phongInfos){
            this.Delete(phongInfo);
        }
    }
    
    public String TaoMaTuDong(String maGoc, int batDau){
        String ma = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.TaoMaTuDong(maGoc, batDau);
        return ma;
    }
    
    public Vector LayDonGiaPhong(String tenPhong) throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("LOAIPHONG.DonGiaPhong, PHONG.MaPhong ", "PHONG, LOAIPHONG", 
                "PHONG.MaLoaiPhong = LOAIPHONG.MaLoaiPhong and PHONG.TenPhong = " + tenPhong, null);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt.add(vt_row);
            
        }
        _connect.Close();
        return vt;
    }
    
    public void Update_TinhTrangPhong(String MaPhong, int isTinhTrangPhong) throws Exception{
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("TinhTrangPhong", isTinhTrangPhong);
        _connect.Update("PHONG", map, "MaPhong = " + MaPhong);
        
        
    }
    
    public Vector LayDanhsachPhong() throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.select_DSphong();
        int stt = 0;
        while (_result.next()){
            Vector vt_row = new Vector();
            stt++;
            vt_row.add(stt);
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt_row.add(_result.getString(5));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector LayDS_TimKiem() throws Exception{
        Vector vt = new Vector();
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.LayDS_TimKiemPhong();
        int stt = 0;
        while (_result.next()){
            Vector vt_row = new Vector();
            stt++;
            vt_row.add(stt);
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector TimKiemPhong(PhongInfo phonginfo, String tenloaiphong, String tinhtrang) throws Exception{
        Vector vt = new Vector();
        String condition = "";
        if(!phonginfo.getTenPhong().equals("")){
            condition += " and TenPhong like '%" + phonginfo.getTenPhong() + "%'";
        }
        
        if(tinhtrang.equals("true")){
            condition += " and TinhTrangPhong = '1'";
        }else if(tinhtrang.equals("false")){
            condition += " and TinhTrangPhong = '0'";
        }else{
            condition += "";
        }
        
        if(!tenloaiphong.equals("")){
            condition += " and TenLoaiPhong like '%" + tenloaiphong.toString() + "%'";
        }
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.TimKiemPhong(condition);
        int stt = 0;
        while (_result.next()){
            Vector vt_row = new Vector();
            stt++;
            vt_row.add(stt);
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public String[] LayTenPhong() throws Exception{
        
        String [] array = new String[100];
        int soItem = 0;
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.LayTenPhong();
        while (_result.next()){
            array[soItem] = (_result.getString(2));
            soItem++;
        }
        String [] array2 = new String[soItem];
        for (int j = 0; j < soItem; j++) {
                array2[j] = array[j].trim();
        }
        _connect.Close();
        return array2;
    }
    
    public String[] LayTenLoaiPhong() throws Exception{
        
        String [] array = new String[100];
        int soItem = 0;
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.LayTenLoaiPhong();
        while (_result.next()){
            array[soItem] = (_result.getString(2));
            soItem++;
        }
        String [] array2 = new String[soItem];
        for (int j = 0; j < soItem; j++) {
                array2[j] = array[j].trim();
        }
        _connect.Close();
        return array2;
    }
    
    public String[] ChuyenTenLP_MaLoaiPhong(String s) throws Exception{
        
        String [] array = new String[100];
        int i = 0;
        int soItem = 0;
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.ChuyenTenLP_MaLoaiPhong(s);
        while (_result.next()){
            array[soItem] = (_result.getString(1));
            soItem++;
        }
        String [] array3 = new String[soItem];
        for (int j = 0; j < soItem; j++) {
                array3[j] = array[j].trim();
        }
        
        _connect.Close();
        return array3;
    }
    
    public String[] LayDonGia(String s) throws Exception{
        String [] array = new String[100];
        int i = 0;
        int soItem = 0;
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.LayDonGia(s);
        while (_result.next()){
            array[soItem] = (_result.getString(1));
            soItem++;
        }
        String [] array3 = new String[soItem];
        for (int j = 0; j < soItem; j++) {
                array3[j] = array[j].trim();
        }
        
        _connect.Close();
        return array3;
    }
    
    public String[] ChuyenTenPhong_MaPhong(String s) throws Exception{
        String [] array = new String[100];
        int i = 0;
        int soItem = 0;
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.ChuyenTenPhong_MaPhong(s);
        while (_result.next()){
            array[soItem] = (_result.getString(1));
            soItem++;
        }
        String [] array3 = new String[soItem];
        for (int j = 0; j < soItem; j++) {
                array3[j] = array[j].trim();
        }
        
        _connect.Close();
        return array3;
    }
    
    
    
}
